<template>
	<div class="tab_box app_background">
		<div class="loginBox">
      <ul>
        <li><a href="javascript:;" class="onActive">欢迎登录！</a></li>
      </ul>
		</div>
		<div class="loginInput">
			<ul>
				<li>
					<p>
            <img src="../../assets/icon/phone.png" alt="">
            <input type="text" placeholder="请输入登陆账号" class="LoginPhone"/>
          </p>
				</li>
				<li>
					<p>
            <img src="../../assets/icon/password.png" alt="">
						<input type="password" placeholder="请输入密码" class="LoginPassWord"/>
					</p>
				</li>
			</ul>
		</div>
		<div class="loginButton">
			<button v-on:click="loginTo" class="Btn_login">登 录</button>
      <!--<button v-on:click="registerTo" class="Btn_register">注 册</button>-->
		</div>

    <loading v-show="FangLoading"></loading>
    <fangerror :fangerroring="fangerroring" :fangerrorText="fangerrorText"></fangerror>

	</div>
</template>

<script>
import {ApiUrl,returnId,EndTokenTime} from '../../../static/js/ApiUrl.js'
import loading from '../common/loading/loading.vue'
import fangerror from '../common/fangerror/fangerror.vue'
export default {
  components: {
    loading,fangerror
  },
  data(){
    return {
      FangLoading:false,//Loading控制，true显示，false不显示
      fangerroring:false,//提示文本弹窗，true显示，false不显示
      fangerrorText:'',//提示文本弹窗内容
    }
  },
  mounted(){
    let access_token = returnId('access_token');
    if(access_token != '' && access_token != null &&  access_token != 'undefined'){
      this.$router.push({path:'index'});
      return;
    }
    $('.select2-dropdown').remove();
    let windowH = $(window).height();
    let meunBoxH = $('.meunBox').outerHeight(true);
    let megessBoxH = $('.megessBox').outerHeight(true);
    $('.contentLeft,.contentRight').height( windowH - meunBoxH - megessBoxH );
    $('.tab_box').height(windowH - meunBoxH);
  },
  methods:{
    setTimeoutError:function () {
       setTimeout(() => {
        $('.Fang_errorBox').hide()
        this.fangerroring = false
       }, 2000)
    },
    loginTo:function(){
      let _This = this;
      var myreg=/^[1][3,4,5,7,8][0-9]{9}$/;
      var LoginPhone = $('.LoginPhone').val();
      var LoginPassWord = $('.LoginPassWord').val();

      if(LoginPhone == ''){
        this.fangerroring = true;
        this.fangerrorText = '账号不能为空'
        this.setTimeoutError()
      }else if(LoginPassWord == ''){
        this.fangerroring = true;
        this.fangerrorText = '密码不能为空'
        this.setTimeoutError()
      }else {
        this.loginAPIData(LoginPhone,LoginPassWord,function (data) {
          console.log(data)
          if(data.retCode == '200'){
            localStorage.setItem('Fangaccess_token',data.results.access_token);
           /* localStorage.setItem('FangTokenTime',new Date().getTime() + 60 * 60 * 1000 * 24 * 30); //设置Token过去时间*/
            _This.$router.push({path: 'index'});
          }
          // console.log(data)
        })
      }
      // this.$router.push({path: 'index'});
//	 			window.location.reload(true);
    },
    loginAPIData:function(loginAccount,loginPws,fn){
      let _This = this;
      _This.FangLoading = true;
      this.axios({
        method: 'post',
        url: ApiUrl() + '/login',
        params: {
          username: loginAccount,
          password:loginPws
        }
      }).then(function (response) {
        if(response.data){
          _This.FangLoading = false;
          if(response.data.loginResult != 0){
            !!fn && fn(response.data);
          }else {
            _This.fangerroring = true;
            _This.fangerrorText = '登录失败'
            _This.setTimeoutError()
          }

        }
      }).catch(function (error) {
        console.log(error);
        _This.FangLoading = false;
      });
    },
    registerTo:function(){
      this.$router.push({path: 'register'});
//	 			window.location.reload(true);
    },
  }
 }

</script>
<style lang="stylus" rel="stylesheet/stylus">
  @import "../common/stylus/meta.styl";
  @import "./login.styl";
</style>
